<template>
  <div class="results-container">
    <!-- 顶部导航栏 -->
    <el-header class="app-header">
      <div class="logo">
        <img src="@/assets/logo.png" alt="Logo" class="logo-image">
      </div>
    </el-header>

    <!-- 主要内容区域 -->
    <el-main class="main-content">
      <el-card shadow="never" class="result-card">
        <template #header>
          <div class="section-header">
            <el-icon :size="18" color="#4caf50"><Document /></el-icon>
            <span class="section-title">识别结果</span>
            <el-button type="text" @click="goBack" class="back-btn">
              <el-icon><ArrowLeft /></el-icon>
              返回
            </el-button>
          </div>
        </template>

        <div class="result-content">
          <div class="main-section">
            <!-- 图片区域 -->
            <el-card shadow="never" class="image-card">
              <template #header>
                <div class="section-header">
                  <el-icon :size="16" color="#4caf50"><Picture /></el-icon>
                  <span>识别结果图</span>
                </div>
              </template>
              <el-image
                  :src="resultImg"
                  fit="contain"
                  class="result-image"
                  :preview-src-list="[resultImg]"
              >
                <template #error>
                  <div class="image-error">
                    <el-icon><Picture /></el-icon>
                    <span>图片加载失败</span>
                  </div>
                </template>
              </el-image>
            </el-card>

            <!-- 信息区域 -->
            <el-card shadow="never" class="info-card">
              <template #header>
                <div class="section-header">
                  <el-icon :size="16" color="#4caf50"><InfoFilled /></el-icon>
                  <span>识别信息</span>
                  <div class="result-switcher" v-if="predictions.length > 1">
                    <el-button
                        size="small"
                        :disabled="currentIndex === 0"
                        @click="switchResult(-1)"
                    >
                      <el-icon><ArrowLeft /></el-icon>
                    </el-button>
                    <span class="result-counter">
                      {{ currentIndex + 1 }}/{{ predictions.length }}
                    </span>
                    <el-button
                        size="small"
                        :disabled="currentIndex === predictions.length - 1"
                        @click="switchResult(1)"
                    >
                      <el-icon><ArrowRight /></el-icon>
                    </el-button>
                  </div>
                </div>
              </template>

              <div class="info-content">
                <el-descriptions :column="1" border>
                  <el-descriptions-item label="算法类型">{{ algorithm }}</el-descriptions-item>
                  <el-descriptions-item label="识别结果">{{ currentResult.result_name || '未知' }}</el-descriptions-item>
                  <el-descriptions-item label="置信度">
                    <el-tag type="success">
                      {{ (currentResult.confidence * 100 || 0).toFixed(2) }}%
                    </el-tag>
                  </el-descriptions-item>
                  <el-descriptions-item label="植物类型">{{ currentResult.plant_type || '未知' }}</el-descriptions-item>
                </el-descriptions>
              </div>
            </el-card>
          </div>

          <!-- 建议区域 -->
          <el-card shadow="never" class="advice-card">
            <template #header>
              <div class="section-header">
                <el-icon :size="16" color="#4caf50"><Comment /></el-icon>
                <span>识别建议</span>
              </div>
            </template>

            <div class="advice-content">
              <el-card v-if="currentAdvice" shadow="never" class="advice-item">
                <div class="advice-content">{{ currentAdvice }}</div>
              </el-card>
              <el-empty v-else description="暂无相关建议" :image-size="80" />
            </div>
          </el-card>
        </div>
      </el-card>
    </el-main>

    <!-- 底部导航栏 -->
    <el-footer class="bottom-nav">
      <el-menu
          mode="horizontal"
          :default-active="activeIndex"
          class="nav-menu"
          @select="handleMenuSelect"
          active-text-color="#4caf50"
          :ellipsis="false"
      >
        <el-menu-item index="/home">
          <template #title>
            <el-icon :size="24"><HomeFilled /></el-icon>
            <span>首页</span>
          </template>
        </el-menu-item>
        <el-menu-item index="/detection">
          <template #title>
            <el-icon :size="24"><Camera /></el-icon>
            <span>识别</span>
          </template>
        </el-menu-item>
        <el-menu-item index="/gallery">
          <template #title>
            <el-icon :size="24"><Notebook /></el-icon>
            <span>图鉴</span>
          </template>
        </el-menu-item>
      </el-menu>
    </el-footer>
  </div>
</template>

<script>
import {
  Document, Picture, InfoFilled, Comment,
  ArrowLeft, ArrowRight, HomeFilled, Camera, Notebook
} from '@element-plus/icons-vue'

export default {
  name: 'RecognitionResults',
  components: {
    Document, Picture, InfoFilled, Comment,
    ArrowLeft, ArrowRight, HomeFilled, Camera, Notebook
  },
  data() {
    return {
      algorithm: '',
      resultImg: '',
      predictions: [],
      allAdvice: [], // 存储所有建议
      currentIndex: 0,
      activeIndex: '/detection'
    }
  },
  computed: {
    currentResult() {
      return this.predictions[this.currentIndex] || {}
    },
    currentAdvice() {
      return this.allAdvice[this.currentIndex] || ""
    }
  },
  created() {
    // 从路由参数获取数据
    const query = this.$route.query
    this.algorithm = query.algorithm || '未知算法'
    this.resultImg = query.resultImg || ''
    this.predictions = query.predictions ? JSON.parse(decodeURIComponent(query.predictions)) : []

    // 解析建议数据，建立与结果的映射关系
    this.allAdvice = query.advice ? JSON.parse(decodeURIComponent(query.advice)) : []

    // 按置信度降序排序
    this.predictions.sort((a, b) => b.confidence - a.confidence)
  },
  methods: {
    goBack() {
      this.$router.back()
    },
    handleMenuSelect(index) {
      this.$router.push(index)
    },
    switchResult(step) {
      const newIndex = this.currentIndex + step
      if (newIndex >= 0 && newIndex < this.predictions.length) {
        this.currentIndex = newIndex
      }
    }
  }
}
</script>

<style scoped>
/* 基础样式与检测界面保持一致 */
.results-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
  background-color: #f8faf8;
  font-family: 'PingFang SC', 'Microsoft YaHei', sans-serif;
}

/* 顶部导航栏 */
.app-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 15px;
  height: 60px;
  background-color: white;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
}

.logo-image {
  height: 40px;
  border-radius: 40px;
}

/* 主要内容区域 */
.main-content {
  flex: 1;
  padding: 15px 15px 70px;
  overflow-y: auto;
  max-width: 1200px;
  margin: 0 auto;
  width: 100%;
}

/* 卡片样式 */
.result-card {
  border-radius: 12px;
  margin-bottom: 20px;
}

.image-card,
.info-card,
.advice-card {
  border-radius: 10px;
  margin-bottom: 20px;
}

/* 分区标题 */
.section-header {
  display: flex;
  align-items: center;
  gap: 8px;
  position: relative;
}

.section-title {
  font-size: 16px;
  font-weight: bold;
  color: #333;
}

.back-btn {
  position: absolute;
  right: 0;
  color: #4caf50;
}

/* 结果切换器 */
.result-switcher {
  position: absolute;
  right: 0;
  display: flex;
  align-items: center;
  gap: 10px;
}

.result-counter {
  font-size: 14px;
  color: #666;
}

/* 主要内容布局 */
.result-content {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.main-section {
  display: grid;
  grid-template-columns: 1.5fr 1fr;
  gap: 20px;
}

/* 图片区域 */
.result-image {
  width: 100%;
  height: 400px;
  border-radius: 8px;
  background-color: #f5f7fa;
}

.image-error {
  display: flex;
  flex-direction: column;
  align-items: center;
  color: #c0c4cc;
}

/* 信息区域 */
.info-content {
  padding: 10px;
}

/* 建议区域 */
.advice-content {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.advice-item {
  border-radius: 6px;
}

/* 底部导航栏 */
.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 0;
  height: 64px;
  background-color: white;
  box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.05);
  z-index: 1000;
}

.nav-menu {
  display: flex;
  justify-content: space-around;
  border: none;
  height: 100%;
}

.nav-menu :deep(.el-menu-item) {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 64px;
  padding: 0;
  border-bottom: none;
  transition: all 0.3s;
}

.nav-menu :deep(.el-menu-item.is-active) {
  color: #4caf50 !important;
  background-color: #e8f5e9 !important;
}

.nav-menu :deep(.el-menu-item .el-icon) {
  margin-right: 0;
  margin-bottom: 6px;
  font-size: 20px;
}

.advice-content {
  padding: 10px;
  color: #666;
  white-space: pre-line;
  line-height: 1.6;
}

/* 响应式调整 */
@media (max-width: 768px) {
  .main-section {
    grid-template-columns: 1fr;
  }

  .result-image {
    height: 300px;
  }

  .main-content {
    padding: 10px 10px 70px;
  }

  .result-switcher {
    position: static;
    margin-left: auto;
  }
}
</style>